<?php

declare(strict_types=1);

namespace app\validate\product;

use think\Validate;

class BrandValidate extends Validate
{
    protected $rule = [
        'id' => 'integer|gt:0',
        'name' => 'max:150',
        'slug' => 'max:150|regex:^[a-z0-9_\-]+$',
        'logo_url' => 'max:255',
        'banner_url' => 'max:255',
        'description' => 'max:500',
        'origin_country' => 'max:80',
        'owner_org_id' => 'integer|gt:0',
        'sort' => 'integer|egt:0',
        'is_visible' => 'in:0,1',
    ];

    protected $message = [
        'id.integer' => '品牌ID格式不正确',
        'id.gt' => '品牌ID格式不正确',
        'name.require' => '品牌名称不能为空',
        'name.max' => '品牌名称不能超过150个字符',
        'slug.max' => '品牌编码不能超过150个字符',
        'slug.regex' => '品牌编码仅支持小写字母、数字、下划线与短横线',
        'logo_url.max' => 'Logo地址不能超过255个字符',
        'banner_url.max' => '横幅地址不能超过255个字符',
        'description.max' => '描述长度不能超过500个字符',
        'origin_country.max' => '发源国家不能超过80个字符',
        'owner_org_id.integer' => '归属组织ID必须为数字',
        'owner_org_id.gt' => '归属组织ID必须大于0',
        'sort.integer' => '排序必须为整数',
        'sort.egt' => '排序不能小于0',
        'is_visible.in' => '展示状态不正确',
    ];

    protected $scene = [
        'create' => [
            'name',
            'slug',
            'logo_url',
            'banner_url',
            'description',
            'origin_country',
            'owner_org_id',
            'sort',
            'is_visible',
        ],
        'update' => [
            'id',
            'name',
            'slug',
            'logo_url',
            'banner_url',
            'description',
            'origin_country',
            'owner_org_id',
            'sort',
            'is_visible',
        ],
    ];

    protected function sceneCreate(): self
    {
        return $this->only($this->scene['create'])
            ->append('name', 'require')
            ->append('slug', 'require|unique:prod_brand,slug');
    }

    protected function sceneUpdate(): self
    {
        return $this->only($this->scene['update'])
            ->append('id', 'require|integer|gt:0')
            ->append('name', 'require');
    }
}
